const express = require('express')
const router = express.Router()
const { insertMsg, getMsg, removeMsg, getFeedback } = require('../../model/mongo')
const multipart = require('../../middleware/content-type/multypart')

router.use(multipart())
router.post('/add', async (req, res) => {
    const { title, description, html } = req.body
    const result = {
        code: 200,
        msg: '消息添加成功'
    }
    result.msg = await insertMsg(title, description, html)
    res.send(JSON.stringify(result))
})

router.get('/feedback', async (req, res) => {
    const result = {
        code: 200,
        msg: '获取用户反馈'
    }
    result.data = await getFeedback()
    res.send(JSON.stringify(result))
})

router.get('/list', async (req, res) => {
    const result = {
        code: 200,
        msg: '获取已发布的消息列表'
    }
    result.data = await getMsg()
    res.send(JSON.stringify(result))
})

router.post('/remove', async (req, res) => {
    const { msgid } = req.body
    const result = {
        code: 200,
        msg: '移除消息'
    }
    result.msg = await removeMsg(msgid)
    res.send(JSON.stringify(result))
})

module.exports = router
